package com.fire.web.modules.all.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.*;
import com.fire.web.modules.all.mapper.AllUserMapper;
import com.fire.web.modules.all.mapper.FireProprietorMapper;
import com.fire.web.modules.all.mapper.FireProprietorUserMapper;
import com.fire.web.modules.all.mapper.UserAuthorityMapper;
import com.fire.web.modules.all.request.AddUserReq;
import com.fire.web.modules.all.service.IFireProprietorUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业主单位用户Service
 *
 * @author Matthew
 * @Date 2019-01-10 11:04
 */
@Service
public class FireProprietorUserServiceImpl extends ServiceImpl<FireProprietorUserMapper, FireProprietorUserModel> implements IFireProprietorUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(FireProprietorUserServiceImpl.class);

    @Autowired
    private FireProprietorUserMapper fireProprietorUserMapper;
    @Autowired
    private FireProprietorMapper fireProprietorMapper;
    @Autowired
    private AllUserMapper allUserMapper;
    @Autowired
    private UserAuthorityMapper userAuthorityMapper;


    @Override
    public FireProprietorUserModel selectOne(FireProprietorUserModel entity){
        return fireProprietorUserMapper.selectOne(entity);
    }

    @Override
    public List<FireProprietorUserModel>selectList(FireProprietorUserModel model){
         return fireProprietorUserMapper.selectList(model, new EntityWrapper<FireProprietorUserModel>());
    }

    @Override
    public List<FireProprietorUserModel>selectList(FireProprietorUserModel model,Wrapper<FireProprietorUserModel>wrapper){
        return fireProprietorUserMapper.selectList(model,wrapper);
    }

    @Override
    public List<FireProprietorUserModel>selectPage(Pagination pagination,FireProprietorUserModel model,Wrapper<FireProprietorUserModel>wrapper){
        return fireProprietorUserMapper.selectPage(pagination,model,wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addProUser(AddUserReq req,String adminid) {
        //先添加t_mit_user获得userid
        AllUserModel allUserModel = new AllUserModel();
        allUserModel.setAliasname(req.getUsername());
		Integer integer = allUserMapper.selectCount(new EntityWrapper<AllUserModel>().eq("phone", req.getPhone()));
		if(integer.intValue()>0){
			return new AjaxResult(ErrorCodeConstants.PHONE_ERROR,"电话号码重复","电话号码重复");
		}
        allUserModel.setPhone(req.getPhone());
        allUserModel.setCreatetime(new Date());
        allUserModel.setUpdatetime(allUserModel.getUpdatetime());
        allUserModel.setChannelid(1);
        allUserModel.setChannelname("fire");
        allUserMapper.insert(allUserModel);
        AllUserModel select = new AllUserModel();
        select.setPhone(req.getPhone());
        allUserModel = allUserMapper.selectOne(select);

        //根据adminid获取业主单位id
        FireProprietorModel fireProprietormodel = new FireProprietorModel();
        fireProprietormodel.setAdminid(adminid);
        fireProprietormodel = fireProprietorMapper.selectOne(fireProprietormodel);

        //插入业主单位用户
        FireProprietorUserModel fireProprietorUserModel = new FireProprietorUserModel();
        fireProprietorUserModel.setUsername(req.getUsername());
        fireProprietorUserModel.setPhone(req.getPhone());
        fireProprietorUserModel.setImageurl(req.getImageurl());
        fireProprietorUserModel.setIdentification(req.getIdentification());
        fireProprietorUserModel.setCreatetime(new Date());
        fireProprietorUserModel.setUpdatetime(fireProprietorUserModel.getCreatetime());
        fireProprietorUserModel.setAdminid(adminid);
        fireProprietorUserModel.setProprietor(fireProprietormodel.getProprietor());
        fireProprietorUserModel.setUserid(allUserModel.getUserid());
        fireProprietorUserMapper.insert(fireProprietorUserModel);
        //插入业主权限
        UserAuthorityModel userAuthorityModel = new UserAuthorityModel();
        userAuthorityModel.setUserid(allUserModel.getUserid());
        userAuthorityModel.setAuthority("proprietor");
        userAuthorityModel.setIsmodifyaccet(req.getIsmodifyaccet());
        userAuthorityModel.setIsmodifydevice(req.getIsmodifydevice());
        userAuthorityModel.setIsmodifysite(req.getIsmodifysite());
        userAuthorityModel.setCreatetime(new Date());
        userAuthorityModel.setUpdatetime(userAuthorityModel.getCreatetime());
        userAuthorityMapper.insert(userAuthorityModel);
        return new AjaxResult(ErrorCodeConstants.SUCCESS,"新增用户成功","新增用户成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateProUser(AddUserReq req) {
        //先更新t_mit_user
        AllUserModel allUserModel = new AllUserModel();
        allUserModel.setUserid(req.getUserid());
        allUserModel.setAliasname(req.getUsername());
        //查询电话是否重复
		Integer integer = allUserMapper.selectCount(new EntityWrapper<AllUserModel>().ne("userid", req.getUserid()).eq("phone", req.getPhone()));
		if(integer.intValue()>0){
			return new AjaxResult(ErrorCodeConstants.PHONE_ERROR,"电话号码重复","电话号码重复");
		}
		allUserModel.setPhone(req.getPhone());
        allUserModel.setUpdatetime(new Date());
        allUserMapper.updateById(allUserModel);

        //再更新业主用户表
        FireProprietorUserModel fireProprietorUserModel = new FireProprietorUserModel();
        fireProprietorUserModel.setUserid(req.getUserid());
        fireProprietorUserModel.setPhone(req.getPhone());
        fireProprietorUserModel.setIdentification(req.getIdentification());
        fireProprietorUserModel.setUsername(req.getUsername());
        fireProprietorUserModel.setImageurl(req.getImageurl());
        fireProprietorUserModel.setUpdatetime(new Date());
        fireProprietorUserMapper.updateById(fireProprietorUserModel);
        //插入业主权限
        UserAuthorityModel userAuthorityModel = new UserAuthorityModel();
        userAuthorityModel.setUserid(allUserModel.getUserid());
        userAuthorityModel.setIsmodifyaccet(req.getIsmodifyaccet());
        userAuthorityModel.setIsmodifydevice(req.getIsmodifydevice());
        userAuthorityModel.setIsmodifysite(req.getIsmodifysite());
        userAuthorityModel.setUpdatetime(new Date());
        userAuthorityMapper.updateById(userAuthorityModel);
        return new AjaxResult(ErrorCodeConstants.SUCCESS,"更新用户成功","更新用户成功");
    }

}
